require('../css/edit.less');
require('../css/commom/basic.css');
document.ready(function () {
  
    let sym = document.querySelector('.sym');
    let nickname = document.querySelector('.nickname');
    let gender = document.querySelector('#gender');
    let concreteGender = document.querySelector('#concreteGender');
    let birthday = document.querySelector('#birthday');
    let concreteBirthday = document.querySelector('#concreteBirthsay');
    let omit = document.querySelector('#omit');
    let concreteOmit = document.querySelector('#concreteOmit');
    let city = document.querySelector('#city');
    let concreteCity = document.querySelector('#concreteCity');
    let concreteMain = document.querySelector('#concreteMain');
    let save = document.querySelector('.save');
    
    let user=JSON.parse(localStorage.getItem('user'));

    let arr={
        userId:user.userId,
        sign:'',
        nickname:'',
        gender:'',
        birthday: '',
        pro: {},
        city: {},
        address :[]

    };
    // 设置默认值
    function approve(array){
        $http.get('/users/accountinfo?userId='+user.userId,function(res){
            nickname.value=res.data.nickname;
            concreteMain.value=res.data.sign;
            concreteGender.value=res.data.gender;
        })
    }
    approve(arr)


    // 返回
    sym.addEventListener('click', function () {
        location.href='./my.html'
    })


    //调用picker  修改性别
    gender.addEventListener('click', function (ev) {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            onConfirm: function (result) {
                //渲染页面
                concreteGender.textContent = result[0].label;
                //存数据
                arr.gender = result[0].label;
            },
            title: '选择性别'
        });
    })
    
    //调用picker 选择生日
    birthday.addEventListener('click', function(ev) {
        weui.datePicker({
            start: 1977,
            end: new Date().getFullYear(),

            onConfirm: function(result) {
                //渲染
                concreteBirthday.textContent = result[0].label + result[1].label + result[2].label;
                //数据
                arr.birthday = result[0].value + '-' + result[1].value + '-' + result[2].value;
                arr.birthday=new Date(arr.birthday).getTime()
            },
            title: '选择生日'
        });
    })

    
    //省 二级联动
    omit.addEventListener('click', function(ev) {
        //请求省级数据
        $http.get('/address/province', function(res) {
            let data = res.data.map(function(item) {
                return {
                    label: item.name,
                    value: item.addressId
                }
            })
            // console.log(data);

            // 生成picker 数据
            weui.picker(data, {
                onConfirm: function(res) {
                    //保存数据
                    arr.pro = res[0];
                    //渲染页面
                    concreteOmit.textContent = res[0].label
                    concreteCity.textContent='请选择城市'
                    cityList(arr.pro);
                    console.log(res[0].value);
                },
                title: '选择省份'
            });

        })
    })
    let cityListT=[];
    // 选择城市
    function cityList(pro){
            $http.get('/address/city/'+pro.value,function(res){
                cityListT=res.data.map(function(item){
                    return {
                        label:item.name,
                        value:item.addressId
                    }
                })
            })
        
    }
    // 渲染到页面
    city.addEventListener('click',function(){
        weui.picker(cityListT, {
            onConfirm: function(res) {
                //保存数据
                arr.city = res[0];
                //渲染页面
                concreteCity.textContent = res[0].label
                cityList(res[0]);
            },
            title: '选择城市'
        });
    })
    // 保存数据
    save.addEventListener('click',function(){
        arr.sign=concreteMain.value;
        arr.nickname=nickname.value;
        arr.address[0]=arr.pro.label;
        arr.address[1]=arr.city.label;

        // 请求保存接口
        $http.post('/users/userEdit',arr,function(res){
            console.log(1);
        })
    })


})